<?php

/**
 * slide actions.
 *
 * @package    beamr
 * @subpackage slide
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class slideActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward404Unless($request->getParameter('token'));
    $this->forward404Unless(Doctrine::getTable('BeamrPresentation')->tokenExist($request->getParameter('token')), 'This presentation does not exist.');

    $this->presentationToken = $request->getParameter('token');
    $this->slides = Doctrine::getTable('BeamrSlide')->getSlidesFromPresentationToken($request->getParameter('token'));
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->presentation = Doctrine::getTable('BeamrPresentation')->findOneByToken($request->getParameter('presentation'));
    $this->form = new BeamrSlideForm(null, array('presentation_id' => $this->presentation->getid(), 'title' => 'toto'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));
    $this->forward404Unless($request->getParameter('presentation'));

    $this->presentation = Doctrine::getTable('BeamrPresentation')->findOneByToken($request->getParameter('presentation'));
    $this->form = new BeamrSlideForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($beamr_slide = Doctrine::getTable('BeamrSlide')->findOneByToken($request->getParameter('token')), 'This slide does not exist.');
    $this->presentation = $beamr_slide->getBeamrPresentation();
    $this->form = new BeamrSlideForm($beamr_slide);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($beamr_slide = Doctrine::getTable('BeamrSlide')->findOneByToken($request->getParameter('token')), 'This slide does not exist.');
    $this->form = new BeamrSlideForm($beamr_slide);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($beamr_slide = Doctrine::getTable('BeamrSlide')->findOneByToken($request->getParameter('token')), 'This slide does not exist.');
    $beamr_slide->delete();

    $this->redirect('@presentation_show?token='. $beamr_slide->getBeamrPresentation()->gettoken());
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      if ($form->getObject()->isNew() && $presentation = Doctrine::getTable('BeamrPresentation')->findOneByToken($request->getParameter('presentation'))) {
        $form->updateObject(array('presentation_id' => $presentation->getId()));
      }
      $beamr_slide = $form->save();

      $this->redirect('@presentation_show?token='. $beamr_slide->getBeamrPresentation()->gettoken());
    }
  }

  public function executeOrder(sfWebRequest $request)
  {
    $slides = $request->getParameter('slide');
    foreach ($slides as $weight => $token) {
      $slide = Doctrine::getTable('BeamrSlide')->findOneByToken($token);
      $slide->setWeight($weight);
      $slide->save(NULL, FALSE);
    }
    return sfView::NONE;
  }
}
